package com.itheima.appserver.controller;


import com.itheima.appserver.service.CommentsService;
import com.itheima.commons.model.vo.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * 评论相关API接口
 */
@RestController
@RequestMapping("comments")
public class CommentController {

    @Autowired
    private CommentsService commentsService;

    /**
     * 查询评论列表
     *
     * @return
     */
    @GetMapping
    public ResponseEntity queryCommentsList(@RequestParam("movementId") String publishId,
                                            @RequestParam(value = "page", defaultValue = "1") Integer page,
                                            @RequestParam(value = "pagesize", defaultValue = "10") Integer pageSize) {
        PageResult pageResult = commentsService.queryCommentList(publishId, page, pageSize);
        return ResponseEntity.ok(pageResult);
    }

    /**
     * 发表评论
     */
    @PostMapping
    public ResponseEntity saveComments(@RequestBody Map<String, String> param) {
        String publishId = param.get("movementId");
        String content = param.get("comment");
        commentsService.publishComment(publishId, content);
        return ResponseEntity.ok(null);
    }

    /**
     * 评论点赞
     *
     * @param publishId 文本评论的ID
     * @return
     */
    @GetMapping("{id}/like")
    public ResponseEntity likeComment(@PathVariable("id") String publishId) {
        Long likeCount = commentsService.commentLike(publishId);
        return ResponseEntity.ok(likeCount);
    }

    /**
     * 评论取消点赞
     *
     * @param publishId
     * @return
     */
    @GetMapping("{id}/dislike")
    public ResponseEntity disLikeComment(@PathVariable("id") String publishId) {
        Long likeCount = commentsService.cancelCommentLike(publishId);
        return ResponseEntity.ok(likeCount);
    }

}
